

#import "TradeSystemDetailRecord.h"
#import "GridTableCell.h"
 

#define LABEL_c0r0 900 
#define LABEL_c0r1 901 
#define LABEL_c1r0 910 
#define LABEL_c1r1 911 
#define LABEL_c2r0 920 
#define LABEL_c2r1 921  
#define LABEL_c3r0 930 
#define LABEL_c3r1 931 
@implementation TradeSystemDetailRecord
@synthesize delegate;
@synthesize delegate_UIPopoverController;
#pragma mark -
#pragma mark Initialization
-(id)initWithTargetSymbol:(DataObject *)adataobject {
	self= [super init];
	if (self ) {
		
		
		stockdata=adataobject;
		
		
	}
	return self;	 	
}


- (void)loadView {
	
	showtradesystemSetUp=YES;
	
	
	
	UIView *mainview;
	mainview=[[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame];
	self.view=mainview;
	self.view.backgroundColor=[UIColor lightTextColor];	
	UINavigationBar *nav;
	
	nav = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];	
	
	UIBarButtonItem *barButton ;
	barButton = [[UIBarButtonItem alloc] initWithTitle:  NSLocalizedString(@"Done",@"完成")  style:UIBarButtonItemStylePlain target:self action:@selector(slideOut)];
	UINavigationItem *item;
	item = [[UINavigationItem alloc] initWithTitle:  @"交易明細"  ];
	[item setRightBarButtonItem: barButton];
	[nav pushNavigationItem:item animated:NO];	
	
	
	NSArray *textoptionarray=[NSArray arrayWithObjects:@"損益表",@"交易記錄",@"損益圖",nil];
	segmetnctl=[[UISegmentedControl alloc] initWithItems:textoptionarray];
	[segmetnctl addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
	segmetnctl.frame=CGRectMake(0, 0, 320, 30);
	item.titleView=segmetnctl;
	segmetnctl.selectedSegmentIndex=0;	
	[segmetnctl autorelease];	
	
 
	//損益表
	gainlossview=[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
	gainlossview.backgroundColor=[UIColor whiteColor];
	gainlossview.hidden=NO;
	[self gainlossShow];
	[self.view addSubview:gainlossview];
	
	
	
	
	//走勢圖
    //走勢圖物件
	graphview=[[UIView alloc] initWithFrame:CGRectMake(0,48, 320, 400)];
	graphview.backgroundColor=[UIColor redColor];
	graphview.hidden=YES;
    [self.view addSubview:graphview];	
 
	Chartview =[[CPTGraphHostingView alloc]initWithFrame:CGRectMake(0,148, 320, 300)];
	graph = [[CPTXYGraph alloc] init ];	
	Chartview.hostedGraph=graph;
	[self.view addSubview:Chartview];	
	Chartview.hidden=YES;			
	
	//背景樣板
	CPTTheme *theme = [CPTTheme themeNamed:kCPTStocksTheme];
    [graph applyTheme:theme];
	
	//邊角
	graph.paddingTop = 1.0;
	graph.paddingBottom = 1.0;
	graph.paddingLeft = 1.0;
	graph.paddingRight =1.0;
	
	//四角圓弧度 越大值表示月圓
	graph.plotAreaFrame.cornerRadius = 0.0f;
	CPTMutableLineStyle *borderLineStyle = [CPTMutableLineStyle lineStyle];
	//邊線顏色
	borderLineStyle.lineColor = [CPTColor yellowColor];
	//邊線的細粗程度
	borderLineStyle.lineWidth = 0.0f;
	//設定邊線款式
	graph.plotAreaFrame.borderLineStyle = borderLineStyle;	
	
	//線圖1
	
	CPTXYPlotSpace *plotSpace1 = [[CPTXYPlotSpace alloc] init];
	[graph addPlotSpace:plotSpace1];	
	
	plotSpace1.delegate=self;	
	
	NSDecimalNumber *lengthDisplayLocation1 =[self HighPrice:@"close"]  ;
	NSDecimalNumber *lowDisplayLocation1 = [NSDecimalNumber decimalNumberWithString:@"0"];  ;
	plotSpace1.yRange = [CPTPlotRange plotRangeWithLocation:[lowDisplayLocation1 decimalValue] length:[lengthDisplayLocation1 decimalValue]];
	plotSpace1.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromUnsignedInteger([stockdata.financialData_TradeSystem count]-1 )];
	
	CPTScatterPlot *dataSourceLinePlot1 = [[[CPTScatterPlot alloc] init] autorelease];
    dataSourceLinePlot1.identifier = @"Plot1";
    
    CPTMutableLineStyle *ALineStyle = [CPTMutableLineStyle lineStyle];
	ALineStyle.lineWidth = 0.5f;
	ALineStyle.lineColor =[CPTColor lightGrayColor];
    
	dataSourceLinePlot1.dataLineStyle=ALineStyle;
    
	
    dataSourceLinePlot1.dataSource = self;	
	[graph addPlot:dataSourceLinePlot1 toPlotSpace:plotSpace1];	
	
	//線圖2
	//預設繪圖區域
    CPTXYPlotSpace *plotSpace2 = (CPTXYPlotSpace *)graph.defaultPlotSpace;
	plotSpace2.delegate=self;	
	
	NSDecimalNumber *length_high_low = [[self HighPrice:@"RUealizeGainLoss"]  decimalNumberBySubtracting:[self LowPrice:@"RUealizeGainLoss"]];
	float intlength;
	intlength=[length_high_low floatValue];
	 
	
	NSDecimalNumber *Percent = [NSDecimalNumber decimalNumberWithString:@"0.05"];	//上下多出來的空間
	NSDecimalNumber *lengthPercent = [length_high_low decimalNumberByMultiplyingBy:Percent];		
	NSDecimalNumber *lowLocation=  [[self LowPrice:@"RUealizeGainLoss"] decimalNumberBySubtracting:lengthPercent]  ;
	NSDecimalNumber *lengthTotal= [[length_high_low decimalNumberByAdding:lengthPercent] decimalNumberByAdding:lengthPercent];	
	
	 
	plotSpace2.yRange = [CPTPlotRange plotRangeWithLocation:[lowLocation decimalValue] length: [lengthTotal decimalValue] ];
	plotSpace2.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromUnsignedInteger([stockdata.financialData_TradeSystem count]-1 )];
	
	CPTScatterPlot *dataSourceLinePlot2 = [[[CPTScatterPlot alloc] init] autorelease];
    dataSourceLinePlot2.identifier = @"Plot2";
	CPTMutableLineStyle *A1LineStyle = [CPTMutableLineStyle lineStyle];
	A1LineStyle.lineWidth = 0.5f;
	A1LineStyle.lineColor =[CPTColor redColor];    
    dataSourceLinePlot2.dataLineStyle  = A1LineStyle;
    
	dataSourceLinePlot2.dataSource = self;	
	[graph addPlot:dataSourceLinePlot2 toPlotSpace:plotSpace2];	
	
    
    
	// Axes
	CPTMutableLineStyle *AxesLineStyle = [CPTMutableLineStyle lineStyle];
	AxesLineStyle.lineWidth =2.0;
	AxesLineStyle.lineColor = [CPTColor blackColor];	
	 
	
	CPTMutableTextStyle *xText = [CPTMutableTextStyle textStyle];
	xText.color = [CPTColor darkGrayColor];	
	xText.fontSize=12;
	
	CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;	
	CPTXYAxis *x = axisSet.xAxis;
	x.orthogonalCoordinateDecimal =[[NSDecimalNumber decimalNumberWithString:@"0"] decimalValue];  //x 軸放在y軸的位置	
	x.axisLineStyle=AxesLineStyle;
	x.labelTextStyle=xText;
	
	CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
	majorGridLineStyle.lineWidth = 0.5;
	majorGridLineStyle.lineColor = [[CPTColor colorWithGenericGray:0.4] colorWithAlphaComponent:0.75];
	
	x.majorGridLineStyle = majorGridLineStyle;
	x.majorIntervalLength =CPTDecimalFromInteger(30);
	x.minorTicksPerInterval = 0;	
	 
    
	//計算間隔多少Ｋ線要呈現日期
	//int showKnumber;
	//showKnumber=6; //走勢圖上秀出6根ＫK線
	int kbarnumber;
	kbarnumber=[stockdata.financialData_TradeSystem count]/6;
	
	 
	NSMutableSet *customTickLocations ;
	customTickLocations=[[NSMutableSet alloc] init];
	
	for (int i=1; i<6; i++) {
		
		if (i*kbarnumber<[stockdata.financialData_TradeSystem count] ) {
			[customTickLocations addObject:[NSDecimalNumber numberWithInt:i*kbarnumber]];	
		}	
		
		
	}
	
	NSMutableArray *xAxisLabels ;
	xAxisLabels=[[NSMutableArray alloc] init];
	for (int i=1; i<6; i++) {
		if (i*kbarnumber<[stockdata.financialData count]) {		
			[xAxisLabels addObject:[self LableDateString: i*kbarnumber]];	
		}
	}	
	 
	NSUInteger labelLocation = 0;
	NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];
	for (NSNumber *tickLocation in customTickLocations) {
		CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:axisSet.xAxis.labelTextStyle];
		newLabel.tickLocation = [tickLocation decimalValue];
		newLabel.offset = axisSet.xAxis.labelOffset + axisSet.xAxis.majorTickLength;
		newLabel.offset =  0.0;
		[customLabels addObject:newLabel];
		[newLabel release];
	}
	 
	x.labelingPolicy = CPTAxisLabelingPolicyNone;
	
	//當使用CPAxisLabelingPolicyNone 可以顯示格線
	axisSet.xAxis.majorTickLocations =	customTickLocations ;
	
	axisSet.xAxis.axisLabels =  [NSSet setWithArray:customLabels];		
	 
	//y
	CPTXYAxis *y = axisSet.yAxis;
	y.axisLineStyle=AxesLineStyle;	
	y.labelTextStyle=xText;	
	
	NSMutableArray *y_customTickLocations =[[NSMutableArray alloc] init];		
	NSMutableArray *y_AxisLabels=[[NSMutableArray alloc] init];	
 
	int y_offset;
	//y軸格線與標示 
    {
		if (intlength<10 && intlength>-10) {
			y_offset=-25;		
			for (int ja=-10; ja<=10; ja=ja+1) {
				[y_customTickLocations addObject:[NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%i",ja]]];		 
				[y_AxisLabels addObject:[NSString stringWithFormat:@"%i",ja]];	
			}		
		}	
		else if (intlength<100 && intlength>-100) {
			y_offset=-30;
			for (int ja=-100; ja<=100; ja=ja+10) {
				[y_customTickLocations addObject:[NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%i",ja]]];		 
				[y_AxisLabels addObject:[NSString stringWithFormat:@"%i",ja]];	
			}		
		}
		else if (intlength<1000 && intlength>-1000) {
			y_offset=-40;
			for (int ja=-1000; ja<=1000; ja=ja+100) {
				[y_customTickLocations addObject:[NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%i",ja]]];		 
				[y_AxisLabels addObject:[NSString stringWithFormat:@"%i",ja]];	
			}		
		}
		else if (intlength<10000 && intlength>-10000) {
			y_offset=-45;
			for (int ja=-10000; ja<=10000; ja=ja+1000) {
				[y_customTickLocations addObject:[NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%i",ja]]];		 
				[y_AxisLabels addObject:[NSString stringWithFormat:@"%i",ja]];	
			}		
		}	
		else if (intlength<100000 && intlength>-100000) {
			y_offset=-50;
			for (int ja=-100000; ja<=100000; ja=ja+10000) {
				[y_customTickLocations addObject:[NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%i",ja]]];		 
				[y_AxisLabels addObject:[NSString stringWithFormat:@"%i",ja]];	
			}		
		}	
		else if (intlength<1000000 && intlength>-1000000) {
			y_offset=-55;
			for (int ja=-1000000; ja<=1000000; ja=ja+100000) {
				[y_customTickLocations addObject:[NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%i",ja]]];		 
				[y_AxisLabels addObject:[NSString stringWithFormat:@"%i",ja]];	
			}		
		}		
		else if (intlength<10000000 && intlength>-10000000) {
			y_offset=-60;
			for (int ja=-10000000; ja<=10000000; ja=ja+1000000) {
				[y_customTickLocations addObject:[NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%i",ja]]];		 
				[y_AxisLabels addObject:[NSString stringWithFormat:@"%i",ja]];	
			}		
		}	
		else if (intlength<100000000 && intlength>-100000000) {
			y_offset=-65;
			for (int ja=-100000000; ja<=100000000; ja=ja+10000000) {
				[y_customTickLocations addObject:[NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%i",ja]]];		 
				[y_AxisLabels addObject:[NSString stringWithFormat:@"%i",ja]];	
			}		
		}	
		
	
	}
	
	NSUInteger y_labelLocation = 0;
	NSMutableArray *y_customLabels = [NSMutableArray arrayWithCapacity:[y_AxisLabels count]];
	for (NSDecimalNumber *tickLocation in y_customTickLocations) {
		
        CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [y_AxisLabels objectAtIndex:y_labelLocation ] textStyle:axisSet.yAxis.labelTextStyle];
		y_labelLocation=y_labelLocation+1;
		
		newLabel.tickLocation = [tickLocation decimalValue];
		newLabel.offset = y_offset;
		[y_customLabels addObject:newLabel];
		[newLabel release];
        
	}
	
	y.labelingPolicy = CPTAxisLabelingPolicyNone;
    
	axisSet.yAxis.axisLabels =  [NSSet setWithArray:y_customLabels];	
	axisSet.yAxis.majorGridLineStyle=majorGridLineStyle;
	
	
	NSMutableSet *yc;
	yc=[y_customTickLocations mutableCopy];
	axisSet.yAxis.majorTickLocations=yc;//y_customTickLocations;
	 
	 
	
	//3
	CPTScatterPlot *dataSourceLinePlot3 = [[[CPTScatterPlot alloc] init] autorelease];
    dataSourceLinePlot3.identifier = @"Plot3";
	 
    CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
    lineStyle.lineWidth = 2.0f;
    lineStyle.lineColor =[CPTColor blueColor];   
    
    dataSourceLinePlot3.dataLineStyle=lineStyle;
    
	dataSourceLinePlot3.dataSource = self;	
	[graph addPlot:dataSourceLinePlot3 toPlotSpace:plotSpace2];		
	
	
	
	//交易記錄
	myTable = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 48.0f, 320.0f, self.view.frame.size.height)  style:UITableViewStylePlain];
	myTable.dataSource = self;	//連結資料 UITableViewDataSource
	myTable.delegate=self; //連結動作 UITableViewDelegate	
	[self.view addSubview:myTable];
	myTable.hidden=YES; 	 	
 	[mainview addSubview:nav ];	
	
	
	
	
	
}

#pragma mark -
#pragma mark  損益表

-(void)gainlossShow{
	
	
	//清除uiview 上所有的view
	for(UIView *subview in [gainlossview subviews]) 
	{
		[subview removeFromSuperview];
		
	}	
	
	//最後一筆
    NSDictionary *tData = (NSDictionary *)[stockdata.financialData_TradeSystem objectAtIndex:[ stockdata.financialData_TradeSystem count]  -1];
	//第一筆
	NSDictionary *tData0 = (NSDictionary *)[stockdata.financialData_TradeSystem objectAtIndex:0];	
	
	NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];		
	
	//詳細表
	UIButton *gainlossshowhide= [UIButton buttonWithType:UIButtonTypeInfoDark];
	gainlossshowhide.frame= CGRectMake(10,30, 20.0, 20.0);
	[gainlossshowhide addTarget:self action:@selector(gainlosssowhidebuttonClicked) forControlEvents:UIControlEventTouchUpInside];	
	[gainlossview addSubview:gainlossshowhide];
	
	int yyy;
	
	if (showtradesystemSetUp) {
        yyy=140;
		
		UILabel *ar0c0=[[UILabel alloc] initWithFrame:CGRectMake(15,50, 70.0, 25.0)];
		ar0c0.font=[UIFont boldSystemFontOfSize:13];
		//r1c2.backgroundColor=[UIColor yellowColor];
		ar0c0.text=@"測試策略:";
		[gainlossview addSubview:ar0c0];		
		[ar0c0 release];
        
		UILabel *ar0c1=[[UILabel alloc] initWithFrame:CGRectMake(85,50, 70.0, 25.0)];
		ar0c1.font=[UIFont boldSystemFontOfSize:13];
		//r1c3.backgroundColor=[UIColor yellowColor];
		ar0c1.text=[prefs objectForKey:@"OptimizeTradeSystem"];
		[gainlossview addSubview:ar0c1];	
		[ar0c1 release];
        
        
		UILabel *ar0c2=[[UILabel alloc] initWithFrame:CGRectMake(155,50, 70.0, 25.0)];
		ar0c2.font=[UIFont boldSystemFontOfSize:13];
		//r1c2.backgroundColor=[UIColor yellowColor];
		ar0c2.text=@"測試週期:";
		[gainlossview addSubview:ar0c2];		
		[ar0c2 release];
		
		UILabel *ar0c3=[[UILabel alloc] initWithFrame:CGRectMake(225,50, 70.0, 25.0)];
		ar0c3.font=[UIFont boldSystemFontOfSize:13];
		//r1c3.backgroundColor=[UIColor yellowColor];
		ar0c3.text=[tData objectForKey:@"dwm"];
		[gainlossview addSubview:ar0c3];			
		[ar0c3 release];
        
		//---------
		UILabel *ar1c0=[[UILabel alloc] initWithFrame:CGRectMake(15,75, 70.0, 25.0)];
		ar1c0.font=[UIFont boldSystemFontOfSize:13];
		//r1c2.backgroundColor=[UIColor yellowColor];
		ar1c0.text=@"停損出場率:";
		[gainlossview addSubview:ar1c0];		
		[ar1c0 release];
        
		UILabel *ar1c1=[[UILabel alloc] initWithFrame:CGRectMake(85,75, 70.0, 25.0)];
		ar1c1.font=[UIFont boldSystemFontOfSize:13];
		//r1c3.backgroundColor=[UIColor yellowColor];
		ar1c1.text=[prefs objectForKey:@"Optimizestopratearay"];
		[gainlossview addSubview:ar1c1];	
		[ar1c1 release];
        
        
		UILabel *ar1c2=[[UILabel alloc] initWithFrame:CGRectMake(155,75, 70.0, 25.0)];
		ar1c2.font=[UIFont boldSystemFontOfSize:13];
		//r1c2.backgroundColor=[UIColor yellowColor];
		ar1c2.text=@"手續費率:";
		[gainlossview addSubview:ar1c2];		
		[ar1c2 release];
        
		UILabel *ar1c3=[[UILabel alloc] initWithFrame:CGRectMake(225,75, 70.0, 25.0)];
		ar1c3.font=[UIFont boldSystemFontOfSize:13];
		//r1c3.backgroundColor=[UIColor yellowColor];
		ar1c3.text=[prefs objectForKey:@"Optimizescostfeeratearay"];
		[gainlossview addSubview:ar1c3];
		[ar1c3 release];
         
         //---------
		UILabel *ar2c0=[[UILabel alloc] initWithFrame:CGRectMake(15,100, 70.0, 25.0)];
		ar2c0.font=[UIFont boldSystemFontOfSize:13];
		//r1c2.backgroundColor=[UIColor yellowColor];
		ar2c0.text=@"進出場價位:";
		[gainlossview addSubview:ar2c0];		
		[ar2c0 release];
        
        
		UILabel *ar2c1=[[UILabel alloc] initWithFrame:CGRectMake(85,100, 70.0, 25.0)];
		ar2c1.font=[UIFont boldSystemFontOfSize:13];
		//r1c3.backgroundColor=[UIColor yellowColor];
		ar2c1.text=[prefs objectForKey:@"enterprice"];
		[gainlossview addSubview:ar2c1];	
		[ar2c1 release];
        
        
		UILabel *ar2c2=[[UILabel alloc] initWithFrame:CGRectMake(155,100, 70.0, 25.0)];
		ar2c2.font=[UIFont boldSystemFontOfSize:13];
		//r1c2.backgroundColor=[UIColor yellowColor];
		ar2c2.text=@"買賣時間點:";
		[gainlossview addSubview:ar2c2];		
		[ar2c2 release];
        
		UILabel *ar2c3=[[UILabel alloc] initWithFrame:CGRectMake(225,100, 70.0, 25.0)];
		ar2c3.font=[UIFont boldSystemFontOfSize:13];
		//r1c3.backgroundColor=[UIColor yellowColor];
		ar2c3.text=[prefs objectForKey:@"enterTime"];
		[gainlossview addSubview:ar2c3];		
		[ar2c3 release];
        
        
		//---------
		UILabel *ar3c0=[[UILabel alloc] initWithFrame:CGRectMake(15,125, 70.0, 25.0)];
		ar3c0.font=[UIFont boldSystemFontOfSize:13];
		//r1c2.backgroundColor=[UIColor yellowColor];
		ar3c0.text=@"買賣類型:";
		[gainlossview addSubview:ar3c0];		
		[ar3c0 release];
        
        
		UILabel *ar3c1=[[UILabel alloc] initWithFrame:CGRectMake(85,125, 70.0, 25.0)];
		ar3c1.font=[UIFont boldSystemFontOfSize:13];
		//r1c3.backgroundColor=[UIColor yellowColor];
		ar3c1.text=[prefs objectForKey:@"buysellboth"];
		[gainlossview addSubview:ar3c1];	
		[ar3c1 release];
        
        
		UILabel *ar3c2=[[UILabel alloc] initWithFrame:CGRectMake(155,125, 70.0, 25.0)];
		ar3c2.font=[UIFont boldSystemFontOfSize:13];
		//r1c2.backgroundColor=[UIColor yellowColor];
		ar3c2.text=@"進出場數量:";
		[gainlossview addSubview:ar3c2];		
		[ar3c2 release];
        
		UILabel *ar3c3=[[UILabel alloc] initWithFrame:CGRectMake(225,125, 70.0, 25.0)];
		ar3c3.font=[UIFont boldSystemFontOfSize:13];
		//r1c3.backgroundColor=[UIColor yellowColor];
		ar3c3.text=[prefs objectForKey:@"enternumber"];
		[gainlossview addSubview:ar3c3];		
		[ar3c3 release];
        
		
	}
	else {
		yyy=0;		
	 
		
	}	
	
	
	
	
	
	UILabel *titl=[[UILabel alloc] initWithFrame:CGRectMake(100,30+yyy, 200.0, 20.0)];
	titl.text= [NSString stringWithFormat: @"損益表:%@",stockdata.symbol];
	
	titl.font=[UIFont boldSystemFontOfSize:15];
	[gainlossview addSubview:titl];
	[titl release];
	UILabel *line1=[[UILabel alloc] initWithFrame:CGRectMake(60,50+yyy, 300.0, 25.0)];
	line1.text=@"--------------------------------------";
	[gainlossview addSubview:line1];
	[line1 release];
    
    UILabel *line1txt=[[UILabel alloc] initWithFrame:CGRectMake(15,50+yyy, 45.0, 25.0)];
	line1txt.text=@"買+賣";
	line1txt.textColor=[UIColor blueColor];
	line1txt.font=[UIFont boldSystemFontOfSize:13];
	[gainlossview addSubview:line1txt];	
	[line1txt release];
	
	UILabel *r0c0=[[UILabel alloc] initWithFrame:CGRectMake(15,70+yyy, 70.0, 25.0)];
	r0c0.font=[UIFont boldSystemFontOfSize:13];
	//r0c0.backgroundColor=[UIColor redColor];
	r0c0.text=@"起始日:";
	[gainlossview addSubview:r0c0];
	[r0c0 release];
    
	UILabel *r0c1=[[UILabel alloc] initWithFrame:CGRectMake(85,70+yyy, 70.0, 25.0)];
	r0c1.font=[UIFont boldSystemFontOfSize:13];
	//r0c1.backgroundColor=[UIColor yellowColor];
	NSDate *startDate= [tData0 objectForKey:@"Tdate"];	
	NSDateFormatter* starta = [[NSDateFormatter alloc]init];
	[starta setDateFormat:@"yyyy/MM/dd"];
	NSString *wstart = [starta stringFromDate:startDate];
    [starta release];
    
	r0c1.text=wstart;
	[gainlossview addSubview:r0c1];	
	[r0c1 release];
    
	UILabel *r0c2=[[UILabel alloc] initWithFrame:CGRectMake(155,70+yyy, 70.0, 25.0)];
	r0c2.font=[UIFont boldSystemFontOfSize:13];
	//r0c2.backgroundColor=[UIColor redColor];
	r0c2.text=@"結束日:";
	[gainlossview addSubview:r0c2];		
	[r0c2 release];
    
    
	UILabel *r0c3=[[UILabel alloc] initWithFrame:CGRectMake(225,70+yyy, 70.0, 25.0)];
	r0c3.font=[UIFont boldSystemFontOfSize:13];
	//r0c3.backgroundColor=[UIColor yellowColor];
	NSDate *theDate= [tData objectForKey:@"Tdate"];	
	NSDateFormatter* mmdd = [[NSDateFormatter alloc]init];
	[mmdd setDateFormat:@"yyyy/MM/dd"];
	NSString *wend = [mmdd stringFromDate:theDate];	
	[mmdd release];
    
    r0c3.text=wend;
	[gainlossview addSubview:r0c3];	
	[r0c3 release];
    
	UILabel *r1c0=[[UILabel alloc] initWithFrame:CGRectMake(15,95+yyy, 70.0, 25.0)];
	r1c0.font=[UIFont boldSystemFontOfSize:13];
	//r1c0.backgroundColor=[UIColor yellowColor];
	r1c0.text=@"年報酬率:";
	[gainlossview addSubview:r1c0];
	[r1c0 release];
    
	UILabel *r1c1=[[UILabel alloc] initWithFrame:CGRectMake(85,95+yyy, 70.0, 25.0)];
	r1c1.font=[UIFont boldSystemFontOfSize:13];
 
	
	if ([[tData objectForKey:@"YearPercentageRealizeGainLoss"] floatValue]<0) {
		r1c1.textColor=[UIColor redColor];
	}
	else {
		r1c1.textColor=[UIColor blackColor];
	}
	
	
	r1c1.text=[NSString stringWithFormat:@"%0.03f", [[tData objectForKey:@"YearPercentageRealizeGainLoss"] floatValue]];
	[gainlossview addSubview:r1c1];	
	[r1c1 release];
    
	UILabel *r1c2=[[UILabel alloc] initWithFrame:CGRectMake(155,95+yyy, 70.0, 25.0)];
	r1c2.font=[UIFont boldSystemFontOfSize:13];
	//r1c2.backgroundColor=[UIColor yellowColor];
	r1c2.text=@"勝率:";
	[gainlossview addSubview:r1c2];		
	[r1c2 release];

	UILabel *r1c3=[[UILabel alloc] initWithFrame:CGRectMake(225,95+yyy, 70.0, 25.0)];
	r1c3.font=[UIFont boldSystemFontOfSize:13];
	//r1c3.backgroundColor=[UIColor yellowColor];
	r1c3.text=[NSString stringWithFormat:@"%0.03f", [[tData objectForKey:@"WinPercentage"] floatValue]];
	[gainlossview addSubview:r1c3];		
	[r1c3 release];
    
	UILabel *r2c0=[[UILabel alloc] initWithFrame:CGRectMake(15,120+yyy, 70.0, 25.0)];
	r2c0.font=[UIFont boldSystemFontOfSize:13];
	//r2c0.backgroundColor=[UIColor yellowColor];
	r2c0.text=@"已實現獲利:";
	[gainlossview addSubview:r2c0];	
	[r2c0 release];
    
	UILabel *r2c1=[[UILabel alloc] initWithFrame:CGRectMake(85,120+yyy, 70.0, 25.0)];
	r2c1.font=[UIFont boldSystemFontOfSize:13];
	if ([[tData objectForKey:@"RealizeGainLoss"] floatValue]<0) {
		 r2c1.textColor=[UIColor redColor];
	}
	else {
		 r2c1.textColor=[UIColor blackColor];
	}
	
	
	
	r2c1.text=[NSString stringWithFormat:@"%0.01f", [[tData objectForKey:@"RealizeGainLoss"] floatValue]];
	[gainlossview addSubview:r2c1];	
	[r2c1 release];
    
	UILabel *r2c2=[[UILabel alloc] initWithFrame:CGRectMake(155,120+yyy, 70.0, 25.0)];
	r2c2.font=[UIFont boldSystemFontOfSize:13];
	//r2c2.backgroundColor=[UIColor yellowColor];
	r2c2.text=@"交易數";
	[gainlossview addSubview:r2c2];		
	[r2c2 release];
    
	UILabel *r2c3=[[UILabel alloc] initWithFrame:CGRectMake(225,120+yyy, 70.0, 25.0)];
	r2c3.font=[UIFont boldSystemFontOfSize:13];
	//r2c3.backgroundColor=[UIColor yellowColor];
	r2c3.text=[NSString stringWithFormat:@"%i", [[tData objectForKey:@"TotalCount"] intValue]];
	[gainlossview addSubview:r2c3];	
	[r2c3 release];
	
	
	UILabel *line2=[[UILabel alloc] initWithFrame:CGRectMake(60,140+yyy, 300.0, 25.0)];
	line2.text=@"--------------------------------------";
	[gainlossview addSubview:line2];
    [line2 release];
    
	UILabel *line2txt=[[UILabel alloc] initWithFrame:CGRectMake(15,140+yyy, 45.0, 25.0)];
	line2txt.text=@"買";
	line2txt.textColor=[UIColor blueColor];
	line2txt.font=[UIFont boldSystemFontOfSize:13];
	[gainlossview addSubview:line2txt];	
	[line2txt release];
	
	UILabel *br1c0=[[UILabel alloc] initWithFrame:CGRectMake(15,160+yyy, 70.0, 25.0)];
	br1c0.font=[UIFont boldSystemFontOfSize:13];
	//br1c0.backgroundColor=[UIColor yellowColor];
	br1c0.text=@"年報酬率:";
	[gainlossview addSubview:br1c0];
	[br1c0 release];
    
    
	UILabel *br1c1=[[UILabel alloc] initWithFrame:CGRectMake(85,160+yyy, 70.0, 25.0)];
	br1c1.font=[UIFont boldSystemFontOfSize:13];
	if ([[tData objectForKey:@"b_YearPercentageRealizeGainLoss"] floatValue]<0) {
		br1c1.textColor=[UIColor redColor];
	}
	else {
		br1c1.textColor=[UIColor blackColor];
	}
	
	
	
	br1c1.text=[NSString stringWithFormat:@"%0.03f", [[tData objectForKey:@"b_YearPercentageRealizeGainLoss"] floatValue]];
	[gainlossview addSubview:br1c1];	
	[br1c1 release];
    
	UILabel *br1c2=[[UILabel alloc] initWithFrame:CGRectMake(155,160+yyy, 70.0, 25.0)];
	br1c2.font=[UIFont boldSystemFontOfSize:13];
	//br1c2.backgroundColor=[UIColor yellowColor];
	br1c2.text=@"勝率:";
	[gainlossview addSubview:br1c2];		
	[br1c2 release];
	
	UILabel *br1c3=[[UILabel alloc] initWithFrame:CGRectMake(225,160+yyy, 70.0, 25.0)];
	br1c3.font=[UIFont boldSystemFontOfSize:13];
	//br1c3.backgroundColor=[UIColor yellowColor];
	br1c3.text=[NSString stringWithFormat:@"%0.03f", [[tData objectForKey:@"b_WinPercentage"] floatValue]];
	[gainlossview addSubview:br1c3];		
	[br1c3 release];
    
	UILabel *br2c0=[[UILabel alloc] initWithFrame:CGRectMake(15,185+yyy, 70.0, 25.0)];
	br2c0.font=[UIFont boldSystemFontOfSize:13];
	//br2c0.backgroundColor=[UIColor yellowColor];
	br2c0.text=@"已實現獲利:";
	[gainlossview addSubview:br2c0];	
	[br2c0 release];
    
	UILabel *br2c1=[[UILabel alloc] initWithFrame:CGRectMake(85,185+yyy, 70.0, 25.0)];
	br2c1.font=[UIFont boldSystemFontOfSize:13];
	 
	if ([[tData objectForKey:@"b_RealizeGainLoss"] floatValue]<0) {
		br2c1.textColor=[UIColor redColor];
	}
	else {
		br2c1.textColor=[UIColor blackColor];
	}

	
	br2c1.text=[NSString stringWithFormat:@"%0.01f", [[tData objectForKey:@"b_RealizeGainLoss"] floatValue]];
	[gainlossview addSubview:br2c1];	
	[br2c1 release];
    
    
	UILabel *br2c2=[[UILabel alloc] initWithFrame:CGRectMake(155,185+yyy, 70.0, 25.0)];
	br2c2.font=[UIFont boldSystemFontOfSize:13];
	//br2c2.backgroundColor=[UIColor yellowColor];
	br2c2.text=@"交易數";
	[gainlossview addSubview:br2c2];		
	[br2c2 release];
    
	UILabel *br2c3=[[UILabel alloc] initWithFrame:CGRectMake(225,185+yyy, 70.0, 25.0)];
	br2c3.font=[UIFont boldSystemFontOfSize:13];
	//br2c3.backgroundColor=[UIColor yellowColor];
	br2c3.text=[NSString stringWithFormat:@"%i", [[tData objectForKey:@"b_TotalCount"] intValue]];
	[gainlossview addSubview:br2c3];	
	[br2c3 release];
    
	UILabel *line3=[[UILabel alloc] initWithFrame:CGRectMake(60,205+yyy, 300.0, 25.0)];
	line3.text=@"--------------------------------------";
	[gainlossview addSubview:line3];
    [line3 release];
    
	UILabel *line3txt=[[UILabel alloc] initWithFrame:CGRectMake(15,205+yyy, 45.0, 25.0)];
	line3txt.text=@"賣";
	line3txt.textColor=[UIColor blueColor];
	line3txt.font=[UIFont boldSystemFontOfSize:13];
	[gainlossview addSubview:line3txt];	
	[line3txt release];
	 
	UILabel *sr1c0=[[UILabel alloc] initWithFrame:CGRectMake(15,225+yyy, 70.0, 25.0)];
	sr1c0.font=[UIFont boldSystemFontOfSize:13];
	//br1c0.backgroundColor=[UIColor yellowColor];
	sr1c0.text=@"年報酬率:";
	[gainlossview addSubview:sr1c0];
	[sr1c0 release];
    
	UILabel *sr1c1=[[UILabel alloc] initWithFrame:CGRectMake(85,225+yyy, 70.0, 25.0)];
	sr1c1.font=[UIFont boldSystemFontOfSize:13];
	if ([[tData objectForKey:@"s_YearPercentageRealizeGainLoss"] floatValue]<0) {
		sr1c1.textColor=[UIColor redColor];
	}
	else {
		sr1c1.textColor=[UIColor blackColor];
	}
	
	
	
	sr1c1.text=[NSString stringWithFormat:@"%0.03f", [[tData objectForKey:@"s_YearPercentageRealizeGainLoss"] floatValue]];
	[gainlossview addSubview:sr1c1];	
	[sr1c1 release];
    
	UILabel *sr1c2=[[UILabel alloc] initWithFrame:CGRectMake(155,225+yyy, 70.0, 25.0)];
	sr1c2.font=[UIFont boldSystemFontOfSize:13];
	//br1c2.backgroundColor=[UIColor yellowColor];
	sr1c2.text=@"勝率:";
	[gainlossview addSubview:sr1c2];		
	[sr1c2 release];
	
	UILabel *sr1c3=[[UILabel alloc] initWithFrame:CGRectMake(225,225+yyy, 70.0, 25.0)];
	sr1c3.font=[UIFont boldSystemFontOfSize:13];
	//br1c3.backgroundColor=[UIColor yellowColor];
	sr1c3.text=[NSString stringWithFormat:@"%0.03f", [[tData objectForKey:@"s_WinPercentage"] floatValue]];
	//NSLog(@"aaaaa=%@",[NSString stringWithFormat:@"%0.03f", [[tData objectForKey:@"s_WinPercentage"] floatValue]]);
	
	[gainlossview addSubview:sr1c3];		
	[sr1c3 release];
    
	UILabel *sr2c0=[[UILabel alloc] initWithFrame:CGRectMake(15,245+yyy, 70.0, 25.0)];
	sr2c0.font=[UIFont boldSystemFontOfSize:13];
	//br2c0.backgroundColor=[UIColor yellowColor];
	sr2c0.text=@"已實現獲利:";
	[gainlossview addSubview:sr2c0];	
	[sr2c0 release];
    
	UILabel *sr2c1=[[UILabel alloc] initWithFrame:CGRectMake(85,245+yyy, 70.0, 25.0)];
	sr2c1.font=[UIFont boldSystemFontOfSize:13];
	
	if ([[tData objectForKey:@"s_RealizeGainLoss"] floatValue]<0) {
		sr2c1.textColor=[UIColor redColor];
	}
	else {
		sr2c1.textColor=[UIColor blackColor];
	}
	
	
	sr2c1.text=[NSString stringWithFormat:@"%0.01f", [[tData objectForKey:@"s_RealizeGainLoss"] floatValue]];
	[gainlossview addSubview:sr2c1];	
	[sr2c1 release];
    
	UILabel *sr2c2=[[UILabel alloc] initWithFrame:CGRectMake(155,245+yyy, 70.0, 25.0)];
	sr2c2.font=[UIFont boldSystemFontOfSize:13];
	//br2c2.backgroundColor=[UIColor yellowColor];
	sr2c2.text=@"交易數";
	[gainlossview addSubview:sr2c2];		
	[sr2c2 release];
    
	UILabel *sr2c3=[[UILabel alloc] initWithFrame:CGRectMake(225,245+yyy, 70.0, 25.0)];
	sr2c3.font=[UIFont boldSystemFontOfSize:13];
	//br2c3.backgroundColor=[UIColor yellowColor];
	sr2c3.text=[NSString stringWithFormat:@"%i", [[tData objectForKey:@"s_TotalCount"] intValue]];
	[gainlossview addSubview:sr2c3];	
    [sr2c3 release];
    
    
	
}



#pragma mark -
#pragma mark  退出至上一層
-(void)slideOut{
	
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        [delegate_UIPopoverController dismissPopoverAnimated:YES]; 
    }    
	else{
        [self.delegate TradeSystemDetailRecordDidFinish:self];    
    }
   	
    
    
}
#pragma mark -
#pragma mark  切換損益顯式模式
-(void) segmentChanged:(id)sender{
	if (segmetnctl==sender) {
		if (segmetnctl.selectedSegmentIndex==0 ) {
			gainlossview.hidden=NO;
			graphview.hidden=YES;
			myTable.hidden=YES;	
			Chartview.hidden=YES;
			[self gainlossShow];
		}
		else if(segmetnctl.selectedSegmentIndex==1){
			gainlossview.hidden=YES;
			graphview.hidden=YES;
			Chartview.hidden=YES;
			myTable.hidden=NO;	
			[myTable reloadData];
		}
		else if(segmetnctl.selectedSegmentIndex==2){
			gainlossview.hidden=YES;
			myTable.hidden=YES;
			Chartview.hidden=NO;
			graphview.hidden=NO;
			NSLog(@"2");		
		}				
		
	}
}		


#pragma mark -
#pragma mark 在記錄表上是否顯式程式交易的設定資料
-(void)sowhidebuttonClicked{
	if (showtradesystemSetUp) {
		showtradesystemSetUp=NO;		
		//NSLog(@"YES");		
	}
	else {
		showtradesystemSetUp=YES;
		//NSLog(@"NO");		
	}

	
	[myTable reloadData];
	
}
#pragma mark -
#pragma mark 在損益表上是否顯式程式交易的設定資料
-(void)gainlosssowhidebuttonClicked{
	if (showtradesystemSetUp) {
		showtradesystemSetUp=NO;		
		 		
	}
	else {
		showtradesystemSetUp=YES;
		 		
	}
	[self gainlossShow];
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
	return [stockdata.financialData count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
	if (showtradesystemSetUp==YES) {
		
		NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
		if ([prefs boolForKey:@"isShowPara"]==YES) {		
			return 150;	
		}
		else {
			return 120;				
			
		}		
		
	}
	else {
		return 30;
	}

	
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
	UIView* customView = [[[UIView alloc] initWithFrame:CGRectMake(30.0, 0.0, 100.0, 200.0)] autorelease];
  	
	
	//最後一筆
    NSDictionary *tData = (NSDictionary *)[stockdata.financialData_TradeSystem objectAtIndex:[ stockdata.financialData_TradeSystem count]  -1];
	//第一筆
	NSDictionary *tData0 = (NSDictionary *)[stockdata.financialData_TradeSystem objectAtIndex:0];	
	
	//決定要使用哪一個程式交易策略
	NSString *Optimizetradesystem;
	NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];	
	
	Optimizetradesystem=[prefs objectForKey:@"OptimizeTradeSystem"]  ;	
	
	//section 底色設定
	customView.backgroundColor = [UIColor lightGrayColor];
	 
	NSString *c0r0;
	NSString *c0r1;
	NSString *c1r0;
	NSString *c1r1;
	NSString *c2r0;
	NSString *c2r1;
	NSString *c3r0;
	NSString *c3r1;
	NSString *r2c0;	
	NSString *r2c1;	
	NSString *r3c0;	
	NSString *r3c1;	
	NSString *r4c0;	
	NSString *r4c1;	
	NSString *r5c0;	
	NSString *r5c1;	
	NSString *r6c0;	
	NSString *r6c1;	
	
	NSString *r7c0;	
	NSString *r7c1;	
	NSString *r8c0;	
	NSString *r8c1;		
	
	//c0r0=@"";
	//c0r1=@"";
	c1r0=@"";
	c1r1=@"";
	//c2r0=@"";
	//c2r1=@"";
	//c3r0=@"";
	//c3r1=@"";
	r4c0=@"停損出場率:";
	r4c1=@"手續費率:";
	r5c0=@"進出場價位:";
	r5c1=@"買賣時間點:";
	r6c0=@"買賣類型:";
	r6c1=@"進出場數量:";	
	
			
	
	
	r2c0=@"測試策略:";
	r2c0=[r2c0 stringByAppendingString:Optimizetradesystem];
	
	r2c1=@"測試週期:";
	r2c1=[r2c1 stringByAppendingString:[tData objectForKey:@"dwm"]];	
	
	r3c0=@"開始:";
	
	NSDate *startDate= [tData0 objectForKey:@"Tdate"];	
	NSDateFormatter* starta = [[NSDateFormatter alloc]init];
	[starta setDateFormat:@"yyyy/MM/dd"];
	NSString *wstart = [starta stringFromDate:startDate];
    [starta release];
    r3c0=[r3c0 stringByAppendingString:wstart];
	
	
	r3c1=@"結束:";
	NSDate *theDate= [tData objectForKey:@"Tdate"];	
	NSDateFormatter* mmdd = [[NSDateFormatter alloc]init];
	[mmdd setDateFormat:@"yyyy/MM/dd"];
    
	NSString *wend = [mmdd stringFromDate:theDate];
	[mmdd release];
    
    r3c1=[r3c1 stringByAppendingString:wend];
	
	r4c0=[r4c0 stringByAppendingString:[prefs objectForKey:@"Optimizestopratearay"]];
	r4c1=[r4c1 stringByAppendingString:[prefs objectForKey:@"Optimizescostfeeratearay"]];
	r5c0=[r5c0 stringByAppendingString:[prefs objectForKey:@"enterprice"]];
	r5c1=[r5c1 stringByAppendingString:[prefs objectForKey:@"enterTime"]];
	r6c0=[r6c0 stringByAppendingString:[prefs objectForKey:@"buysellboth"]];
	r6c1=[r6c1 stringByAppendingString:[prefs objectForKey:@"enternumber"]];		 
	 	
	if ([prefs boolForKey:@"isShowPara"]==YES) {
		
		if ([Optimizetradesystem isEqualToString:@"MESA"]) {
			r7c0=[NSString stringWithFormat:@"FastLim:%0.03F",[[tData objectForKey:@"FastLim"] floatValue]];
			r7c1=[NSString stringWithFormat:@"SlowLim:%0.03F",[[tData objectForKey:@"SlowLim"]  floatValue]];
			r8c0=@"";
			r8c1=@"";	
			
			
			
		} 
		else if([Optimizetradesystem isEqualToString:@"LF"]) {
			r7c0=[NSString stringWithFormat:@"BRangeDay:%i",[[tData objectForKey:@"BRangeDay"] intValue]];
			r7c1=[NSString stringWithFormat:@"SRangeDay:%i",[[tData objectForKey:@"SRangeDay"] intValue]];
			r8c0=[NSString stringWithFormat:@"Bratio:%0.03f",[[tData objectForKey:@"Bratio"] floatValue]];
			r8c1=[NSString stringWithFormat:@"Sratio:%0.03f",[[tData objectForKey:@"Sratio"] floatValue]];	
			
			
		}	
		
		
		
		
	}
	else {
		r7c0=@"";
		r7c1=@"";
		r8c0=@"";
		r8c1=@"";	
		
		
		
	}

	

	
	
	c0r0=@"月/日";
	c0r1=@"年";
	
	

	
	
	 
	if ([Optimizetradesystem isEqualToString:@"MESA"]) {
		c1r0= @"FAMA";	
		c1r1= @"MAMA";		 
		
	} 
	else if([Optimizetradesystem isEqualToString:@"LF"]) {
		c1r0= @"LF BS";	
		c1r1= @"收盤價";
	}	
	
	
	c2r0= @"買賣狀態" ;	
	c2r1=@"部位";
	c3r0=@"浮動損益" ;
	c3r1= @"累積損益";
	
	int starty;
	if (showtradesystemSetUp==YES) {
		
		if ([prefs boolForKey:@"isShowPara"]==YES) {		
		  starty=115;
		}
		else {
		  starty=85;			
			
		}

		
		
		UILabel * headerLabel_r2c0 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
		headerLabel_r2c0.backgroundColor = [UIColor clearColor];
		headerLabel_r2c0.opaque = NO;
		headerLabel_r2c0.textColor = [UIColor blueColor];
		headerLabel_r2c0.font = [UIFont boldSystemFontOfSize:12];
		headerLabel_r2c0.frame = CGRectMake(10,-5, 100.0, 30.0);
		headerLabel_r2c0.textAlignment = UITextAlignmentLeft;
		headerLabel_r2c0.text = [NSString stringWithString:r2c0 ];
		headerLabel_r2c0.alpha=1;	
		[customView addSubview:headerLabel_r2c0];
		
		UILabel * headerLabel_r2c1 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
		headerLabel_r2c1.backgroundColor = [UIColor clearColor];
		headerLabel_r2c1.opaque = NO;
		headerLabel_r2c1.textColor = [UIColor blueColor];
		headerLabel_r2c1.font = [UIFont boldSystemFontOfSize:12];
		headerLabel_r2c1.frame = CGRectMake(180,-5, 100.0, 30.0);
		headerLabel_r2c1.textAlignment = UITextAlignmentLeft;
		headerLabel_r2c1.text = [NSString stringWithString:r2c1 ];
		headerLabel_r2c1.alpha=1;	
		[customView addSubview:headerLabel_r2c1];
		
		UILabel * headerLabel_r3c0 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
		headerLabel_r3c0.backgroundColor = [UIColor clearColor];
		headerLabel_r3c0.opaque = NO;
		headerLabel_r3c0.textColor = [UIColor blueColor];
		headerLabel_r3c0.font = [UIFont boldSystemFontOfSize:12];
		headerLabel_r3c0.frame = CGRectMake(10,10, 100.0, 30.0);
		headerLabel_r3c0.textAlignment = UITextAlignmentLeft;
		headerLabel_r3c0.text = [NSString stringWithString:r3c0 ];
		headerLabel_r3c0.alpha=1;	
		[customView addSubview:headerLabel_r3c0];
		
		UILabel * headerLabel_r3c1 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
		headerLabel_r3c1.backgroundColor = [UIColor clearColor];
		headerLabel_r3c1.opaque = NO;
		headerLabel_r3c1.textColor = [UIColor blueColor];
		headerLabel_r3c1.font = [UIFont boldSystemFontOfSize:12];
		headerLabel_r3c1.frame = CGRectMake(180,10, 100.0, 30.0);
		headerLabel_r3c1.textAlignment = UITextAlignmentLeft;
		headerLabel_r3c1.text = [NSString stringWithString:r3c1 ];
		headerLabel_r3c1.alpha=1;	
		[customView addSubview:headerLabel_r3c1];	
		
		UILabel * headerLabel_r4c0 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
		headerLabel_r4c0.backgroundColor = [UIColor clearColor];
		headerLabel_r4c0.opaque = NO;
		headerLabel_r4c0.textColor = [UIColor blueColor];
		headerLabel_r4c0.font = [UIFont boldSystemFontOfSize:12];
		headerLabel_r4c0.frame = CGRectMake(10,25, 200.0, 30.0);
		headerLabel_r4c0.textAlignment = UITextAlignmentLeft;
		headerLabel_r4c0.text = [NSString stringWithString:r4c0 ];
		headerLabel_r4c0.alpha=1;	
		[customView addSubview:headerLabel_r4c0];
		
		UILabel * headerLabel_r4c1 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
		headerLabel_r4c1.backgroundColor = [UIColor clearColor];
		headerLabel_r4c1.opaque = NO;
		headerLabel_r4c1.textColor = [UIColor blueColor];
		headerLabel_r4c1.font = [UIFont boldSystemFontOfSize:12];
		headerLabel_r4c1.frame = CGRectMake(180,25, 200.0, 30.0);
		headerLabel_r4c1.textAlignment = UITextAlignmentLeft;
		headerLabel_r4c1.text = [NSString stringWithString:r4c1 ];
		headerLabel_r4c1.alpha=1;	
		[customView addSubview:headerLabel_r4c1];		
		
		UILabel * headerLabel_r5c0 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
		headerLabel_r5c0.backgroundColor = [UIColor clearColor];
		headerLabel_r5c0.opaque = NO;
		headerLabel_r5c0.textColor = [UIColor blueColor];
		headerLabel_r5c0.font = [UIFont boldSystemFontOfSize:12];
		headerLabel_r5c0.frame = CGRectMake(10,40, 200.0, 30.0);
		headerLabel_r5c0.textAlignment = UITextAlignmentLeft;
		headerLabel_r5c0.text = [NSString stringWithString:r5c0 ];
		headerLabel_r5c0.alpha=1;	
		[customView addSubview:headerLabel_r5c0];
		
		UILabel * headerLabel_r5c1 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
		headerLabel_r5c1.backgroundColor = [UIColor clearColor];
		headerLabel_r5c1.opaque = NO;
		headerLabel_r5c1.textColor = [UIColor blueColor];
		headerLabel_r5c1.font = [UIFont boldSystemFontOfSize:12];
		headerLabel_r5c1.frame = CGRectMake(180,40, 200.0, 30.0);
		headerLabel_r5c1.textAlignment = UITextAlignmentLeft;
		headerLabel_r5c1.text = [NSString stringWithString:r5c1 ];
		headerLabel_r5c1.alpha=1;	
		[customView addSubview:headerLabel_r5c1];		
		
		UILabel * headerLabel_r6c0 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
		headerLabel_r6c0.backgroundColor = [UIColor clearColor];
		headerLabel_r6c0.opaque = NO;
		headerLabel_r6c0.textColor = [UIColor blueColor];
		headerLabel_r6c0.font = [UIFont boldSystemFontOfSize:12];
		headerLabel_r6c0.frame = CGRectMake(10,55, 200.0, 30.0);
		headerLabel_r6c0.textAlignment = UITextAlignmentLeft;
		headerLabel_r6c0.text = [NSString stringWithString:r6c0 ];
		headerLabel_r6c0.alpha=1;	
		[customView addSubview:headerLabel_r6c0];
		
		UILabel * headerLabel_r6c1 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
		headerLabel_r6c1.backgroundColor = [UIColor clearColor];
		headerLabel_r6c1.opaque = NO;
		headerLabel_r6c1.textColor = [UIColor blueColor];
		headerLabel_r6c1.font = [UIFont boldSystemFontOfSize:12];
		headerLabel_r6c1.frame = CGRectMake(180,55, 200.0, 30.0);
		headerLabel_r6c1.textAlignment = UITextAlignmentLeft;
		headerLabel_r6c1.text = [NSString stringWithString:r6c1 ];
		headerLabel_r6c1.alpha=1;	
		[customView addSubview:headerLabel_r6c1];		
		
		UILabel * headerLabel_r7c0 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
		headerLabel_r7c0.backgroundColor = [UIColor clearColor];
		headerLabel_r7c0.opaque = NO;
		headerLabel_r7c0.textColor = [UIColor blueColor];
		headerLabel_r7c0.font = [UIFont boldSystemFontOfSize:12];
		headerLabel_r7c0.frame = CGRectMake(10,70, 200.0, 30.0);
		headerLabel_r7c0.textAlignment = UITextAlignmentLeft;
		headerLabel_r7c0.text = [NSString stringWithString:r7c0 ];
		headerLabel_r7c0.alpha=1;	
		[customView addSubview:headerLabel_r7c0];
		
		UILabel * headerLabel_r7c1 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
		headerLabel_r7c1.backgroundColor = [UIColor clearColor];
		headerLabel_r7c1.opaque = NO;
		headerLabel_r7c1.textColor = [UIColor blueColor];
		headerLabel_r7c1.font = [UIFont boldSystemFontOfSize:12];
		headerLabel_r7c1.frame = CGRectMake(180,70, 200.0, 30.0);
		headerLabel_r7c1.textAlignment = UITextAlignmentLeft;
		headerLabel_r7c1.text = [NSString stringWithString:r7c1 ];
		headerLabel_r7c1.alpha=1;	
		[customView addSubview:headerLabel_r7c1];	
		
		UILabel * headerLabel_r8c0 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
		headerLabel_r8c0.backgroundColor = [UIColor clearColor];
		headerLabel_r8c0.opaque = NO;
		headerLabel_r8c0.textColor = [UIColor blueColor];
		headerLabel_r8c0.font = [UIFont boldSystemFontOfSize:12];
		headerLabel_r8c0.frame = CGRectMake(10,85, 200.0, 30.0);
		headerLabel_r8c0.textAlignment = UITextAlignmentLeft;
		headerLabel_r8c0.text = [NSString stringWithString:r8c0 ];
		headerLabel_r8c0.alpha=1;	
		[customView addSubview:headerLabel_r8c0];
		
		UILabel * headerLabel_r8c1 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
		headerLabel_r8c1.backgroundColor = [UIColor clearColor];
		headerLabel_r8c1.opaque = NO;
		headerLabel_r8c1.textColor = [UIColor blueColor];
		headerLabel_r8c1.font = [UIFont boldSystemFontOfSize:12];
		headerLabel_r8c1.frame = CGRectMake(180,85, 200.0, 30.0);
		headerLabel_r8c1.textAlignment = UITextAlignmentLeft;
		headerLabel_r8c1.text = [NSString stringWithString:r8c1 ];
		headerLabel_r8c1.alpha=1;	
		[customView addSubview:headerLabel_r8c1];	
		
		
		
		
	}
	else {
		starty=0;		
		
	}

	
		//===========================
	
	UIButton *showhide= [UIButton buttonWithType:UIButtonTypeInfoLight];
	showhide.frame= CGRectMake(0,2+starty, 20.0, 20.0);
	[showhide addTarget:self action:@selector(sowhidebuttonClicked) forControlEvents:UIControlEventTouchUpInside];	
	
	[customView addSubview:showhide];
	
	UILabel * headerLabel_c0r0 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
	headerLabel_c0r0.backgroundColor = [UIColor clearColor];
	headerLabel_c0r0.opaque = NO;
	headerLabel_c0r0.textColor = [UIColor blueColor];
	headerLabel_c0r0.font = [UIFont boldSystemFontOfSize:12];
	headerLabel_c0r0.frame = CGRectMake(20,-10+starty, 100.0, 30.0);
	headerLabel_c0r0.textAlignment = UITextAlignmentLeft;
	headerLabel_c0r0.text = [NSString stringWithString:c0r0 ];
	headerLabel_c0r0.alpha=1;	
	[customView addSubview:headerLabel_c0r0];
	
	UILabel * headerLabel_c0r1 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
	headerLabel_c0r1.backgroundColor = [UIColor clearColor];
	headerLabel_c0r1.opaque = NO;
	headerLabel_c0r1.textColor = [UIColor redColor];
	headerLabel_c0r1.font = [UIFont boldSystemFontOfSize:12];
	headerLabel_c0r1.frame = CGRectMake(20,5+starty, 100.0, 30.0);
	headerLabel_c0r1.textAlignment = UITextAlignmentLeft;
	headerLabel_c0r1.text = [NSString stringWithString: c0r1 ];
	headerLabel_c0r1.alpha=1;	
	[customView addSubview:headerLabel_c0r1];	
	
	UILabel * headerLabel_c1r0 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
	headerLabel_c1r0.backgroundColor = [UIColor clearColor];
	headerLabel_c1r0.opaque = NO;
	headerLabel_c1r0.textColor = [UIColor blueColor];
	headerLabel_c1r0.font = [UIFont boldSystemFontOfSize:12];
	headerLabel_c1r0.frame = CGRectMake(100,-10+starty, 100.0, 30.0);
	headerLabel_c1r0.textAlignment = UITextAlignmentLeft;
	headerLabel_c1r0.text = [NSString stringWithString:c1r0 ];
	headerLabel_c1r0.alpha=1;	
	[customView addSubview:headerLabel_c1r0];			  
	
	UILabel * headerLabel_c1r1 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
	headerLabel_c1r1.backgroundColor = [UIColor clearColor];
	headerLabel_c1r1.opaque = NO;
	headerLabel_c1r1.textColor = [UIColor redColor];
	headerLabel_c1r1.font = [UIFont boldSystemFontOfSize:12];
	headerLabel_c1r1.frame = CGRectMake(100,5+starty, 100.0, 30.0);
	headerLabel_c1r1.textAlignment = UITextAlignmentLeft;
	headerLabel_c1r1.text = [NSString stringWithString:c1r1];
	headerLabel_c1r1.alpha=1;	
	[customView addSubview:headerLabel_c1r1];		  
	
	
	UILabel * headerLabel_c2r0 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
	headerLabel_c2r0.backgroundColor = [UIColor clearColor];
	headerLabel_c2r0.opaque = NO;
	headerLabel_c2r0.textColor = [UIColor blueColor];
	headerLabel_c2r0.font = [UIFont boldSystemFontOfSize:12];
	headerLabel_c2r0.frame = CGRectMake(180,-10+starty, 100.0, 30.0);
	headerLabel_c2r0.textAlignment = UITextAlignmentLeft;
	headerLabel_c2r0.text = [NSString stringWithString:c2r0];
	headerLabel_c2r0.alpha=1;	
	[customView addSubview:headerLabel_c2r0];			  
	
	UILabel * headerLabel_c2r1 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
	headerLabel_c2r1.backgroundColor = [UIColor clearColor];
	headerLabel_c2r1.opaque = NO;
	headerLabel_c2r1.textColor = [UIColor redColor];
	headerLabel_c2r1.font = [UIFont boldSystemFontOfSize:12];
	headerLabel_c2r1.frame = CGRectMake(180,5+starty, 100.0, 30.0);
	headerLabel_c2r1.textAlignment = UITextAlignmentLeft;
	headerLabel_c2r1.text = [NSString stringWithString:c2r1  ];
	headerLabel_c2r1.alpha=1;	
	[customView addSubview:headerLabel_c2r1];		
	
	
	UILabel * headerLabel_c3r0 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
	headerLabel_c3r0.backgroundColor = [UIColor clearColor];
	headerLabel_c3r0.opaque = NO;
	headerLabel_c3r0.textColor = [UIColor blueColor];
	headerLabel_c3r0.font = [UIFont boldSystemFontOfSize:12];
	headerLabel_c3r0.frame = CGRectMake(260,-10+starty, 100.0, 30.0);
	headerLabel_c3r0.textAlignment = UITextAlignmentLeft;
	headerLabel_c3r0.text = [NSString stringWithString:c3r0 ];
	headerLabel_c3r0.alpha=1;	
	[customView addSubview:headerLabel_c3r0];			
	
	UILabel * headerLabel_c3r1 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
	headerLabel_c3r1.backgroundColor = [UIColor clearColor];
	headerLabel_c3r1.opaque = NO;
	headerLabel_c3r1.textColor = [UIColor blueColor];
	headerLabel_c3r1.font = [UIFont boldSystemFontOfSize:12];
	headerLabel_c3r1.frame = CGRectMake(260,5+starty, 100.0, 30.0);
	headerLabel_c3r1.textAlignment = UITextAlignmentLeft;
	headerLabel_c3r1.text = [NSString stringWithString:c3r1 ];
	headerLabel_c3r1.alpha=1;	
	[customView addSubview:headerLabel_c3r1];		
	
	
	
	
	
	
	
	
	
	return customView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	//NSLog(@"DDDDD=%i",[ stockdata.financialData_TradeSystem count] -indexPath.row-1 );
	
	NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row];
	GridTableCell *cell = (GridTableCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	
	if (cell == nil) {
		cell = [[[GridTableCell alloc] initWithFrame:CGRectZero ] autorelease];
		
		if ([ stockdata.financialData_TradeSystem count] -indexPath.row-1>15) {				
			NSArray *tradesystem=stockdata.financialData_TradeSystem;
			
			NSDictionary *tData = (NSDictionary *)[tradesystem objectAtIndex:[ stockdata.financialData_TradeSystem count] -indexPath.row-1];
				
			
			NSDate *theDate= [tData objectForKey:@"Tdate"];	
			
			NSDateFormatter* mmdd = [[NSDateFormatter alloc]init];
			[mmdd setDateFormat:@"MM/dd"];
			NSString *smmdd = [mmdd stringFromDate:theDate];
			[mmdd release];
            
            NSDateFormatter* yyyy = [[NSDateFormatter alloc]init];
			[yyyy setDateFormat:@"yyyy"];
			NSString *syyyy = [yyyy stringFromDate:theDate];		
			[yyyy release];
            
			
			[cell addColumn:80];
			[cell addColumn:160];		
			[cell addColumn:240];		
			[cell addColumn:320];		
			
			
			NSString *c0r0;
			NSString *c0r1;
			NSString *c1r0;
			NSString *c1r1;
			NSString *c2r0;
			NSString *c2r1;
			NSString *c3r0;
			NSString *c3r1;
			
			
			c0r0=smmdd;
			c0r1=syyyy;
			
			//決定要使用哪一個程式交易策略
			NSString *Optimizetradesystem;
			NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];	
			
			Optimizetradesystem=[prefs objectForKey:@"OptimizeTradeSystem"]  ;
			
			
			if ([Optimizetradesystem isEqualToString:@"MESA"]) {
				
				c1r0= [NSString stringWithFormat:@"%0.03f", [[tData objectForKey:@"Tfama"] floatValue]];	
				c1r1= [NSString stringWithFormat:@"%0.03f", [[tData objectForKey:@"Tmama"] floatValue]];
				
				
			} 
			else if([Optimizetradesystem isEqualToString:@"LF"]) {
				//先決定現在是多頭停損線
				//NSLog(@"aaa===%@",[tData objectForKey:@"LF_b"] )	;	
				if ([[tData objectForKey:@"LF_b"] intValue]==1) {
					c1r0= [NSString stringWithFormat:@"B%0.03f", [[tData objectForKey:@"LF_bout"] floatValue]];	
					c1r1= [NSString stringWithFormat:@"%0.02f", [[tData objectForKey:@"close"] floatValue]];
				}
				if ([[tData objectForKey:@"LF_s"] intValue]==1) {
					c1r0= [NSString stringWithFormat:@"S%0.03f", [[tData objectForKey:@"LF_sout"] floatValue]];	
					c1r1= [NSString stringWithFormat:@"%0.02f", [[tData objectForKey:@"close"] floatValue]];
				}
				
			}
			
			
			
			c2r0= [tData objectForKey:@"BSsignalNextDay"]  ;	
			c2r1= [NSString stringWithFormat:@"%i", [[tData objectForKey:@"Position"] intValue]];
			c3r0= [NSString stringWithFormat:@"%0.03f", [[tData objectForKey:@"unrealizeGainLoss"] floatValue]];
			c3r1=[NSString stringWithFormat:@"%0.03f", [[tData objectForKey:@"RUealizeGainLoss"] floatValue]] ;
			
			
			
			
			
			
			UILabel *label = [[[UILabel	alloc] initWithFrame:CGRectMake(0.0, 0,50.0, tableView.rowHeight/2)] autorelease]; 
			label.tag =  LABEL_c0r0; 
			label.font= [UIFont systemFontOfSize:13.0]; 
			label.text=c0r0;
			label.textAlignment = UITextAlignmentRight; 
			label.textColor = [UIColor blueColor]; 
			label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
			
			UIViewAutoresizingFlexibleHeight; 
			[cell.contentView addSubview:label]; 
			
			label =  [[[UILabel	alloc] initWithFrame:CGRectMake( 0.0, 20,50.0, tableView.rowHeight/2)] autorelease]; 
			label.tag =  LABEL_c0r1; 
			label.font = [UIFont systemFontOfSize:13.0]; 
			label.text =c0r1 ; 
			label.textAlignment = UITextAlignmentRight; 
			label.textColor = [UIColor redColor]; 
			label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
			UIViewAutoresizingFlexibleHeight; 
			[cell.contentView addSubview:label]; 
			
			
			
			
			
			label =  [[[UILabel	alloc] initWithFrame:CGRectMake(90.0, 0, 50.0, tableView.rowHeight/2) ] autorelease]; 		
			label.tag =  LABEL_c1r0; 
			label.font = [UIFont systemFontOfSize:13.0]; 
			// add some silly value
			label.text =c1r0 ;
			label.textAlignment = UITextAlignmentRight; 
			label.textColor = [UIColor blueColor]; 
			label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
			UIViewAutoresizingFlexibleHeight; 
			[cell.contentView addSubview:label];
			
			
			
			label =  [[[UILabel	alloc] initWithFrame:CGRectMake(90.0, 20, 50.0, tableView.rowHeight/2) ] autorelease]; 		
			label.tag = LABEL_c1r1; 
			label.font = [UIFont systemFontOfSize:13.0]; 
			// add some silly value
			label.text =c1r1 ;
			label.textAlignment = UITextAlignmentRight; 
			label.textColor = [UIColor redColor]; 
			label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
			UIViewAutoresizingFlexibleHeight; 
			[cell.contentView addSubview:label];
			
			
			label =  [[[UILabel	alloc] initWithFrame:CGRectMake(170.0, 0, 50.0, tableView.rowHeight/2) ] autorelease]; 		
			label.tag =  LABEL_c2r0; 
			label.font = [UIFont systemFontOfSize:13.0]; 
			// add some silly value
			label.text = c2r0 ;
			label.textAlignment = UITextAlignmentRight; 
			label.textColor = [UIColor blueColor]; 
			label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
			UIViewAutoresizingFlexibleHeight; 
			[cell.contentView addSubview:label];
			
			
			
			label =  [[[UILabel	alloc] initWithFrame:CGRectMake(170.0, 20, 50.0, tableView.rowHeight/2) ] autorelease]; 		
			label.tag = LABEL_c2r1; 
			label.font = [UIFont systemFontOfSize:13.0]; 
			// add some silly value
			label.text = c2r1;
			label.textAlignment = UITextAlignmentRight; 
			label.textColor = [UIColor redColor]; 
			label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
			UIViewAutoresizingFlexibleHeight; 
			[cell.contentView addSubview:label];		
			
			
			label =  [[[UILabel	alloc] initWithFrame:CGRectMake(240.0, 0, 70.0, tableView.rowHeight/2) ] autorelease]; 		
			label.tag = LABEL_c3r0; 
			label.font = [UIFont systemFontOfSize:11.0]; 
			// add some silly value
			label.text =c3r0;
			label.textAlignment = UITextAlignmentRight; 
			label.textColor = [UIColor blueColor]; 
			label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
			UIViewAutoresizingFlexibleHeight; 
			[cell.contentView addSubview:label];		
			
			label =  [[[UILabel	alloc] initWithFrame:CGRectMake(240.0, 20, 70.0, tableView.rowHeight/2) ] autorelease]; 		
			label.tag = LABEL_c3r1; 
			label.font = [UIFont systemFontOfSize:11.0]; 
			// add some silly value
			label.text =c3r1;
			label.textAlignment = UITextAlignmentRight; 
			label.textColor = [UIColor blueColor]; 
			label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
			UIViewAutoresizingFlexibleHeight; 
			[cell.contentView addSubview:label];			
			
		} 
		
				
		
	}
	
	return cell;
}

 #pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
    /*
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
    */
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


#pragma mark -
#pragma mark  走勢圖資料匯入委託方法
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot{
	 
	if ([(NSString *)plot.identifier isEqualToString:@"Plot1"]) {
		return [stockdata.financialData_TradeSystem count]-1;
	}
	else if ([(NSString *)plot.identifier isEqualToString:@"Plot2"]) {
		return [stockdata.financialData_TradeSystem count]-1;
	}	
	else if ([(NSString *)plot.identifier isEqualToString:@"Plot3"]) {
		return [stockdata.financialData_TradeSystem count]-1;
	}	
	
	else {
		return 0;
	}

   
		
	 
	
}
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
	
	 
	NSDecimalNumber *num = [NSDecimalNumber zero];		
	 
	if ([(NSString *)plot.identifier isEqualToString:@"Plot1"]) {
		if ( fieldEnum == CPTScatterPlotFieldY ) {
			
			NSDictionary *tData = (NSDictionary *)[stockdata.financialData_TradeSystem objectAtIndex:index];
			num = (NSDecimalNumber *)[NSDecimalNumber numberWithFloat:[[tData objectForKey:@"close"] floatValue]];		
			
		}
		if(fieldEnum== CPTTradingRangePlotFieldX) 
			num = (NSDecimalNumber *) [NSDecimalNumber numberWithInt:index];		
	}
	if ([(NSString *)plot.identifier isEqualToString:@"Plot2"]) {
		if ( fieldEnum == CPTScatterPlotFieldY ) {
			
			NSDictionary *tData = (NSDictionary *)[stockdata.financialData_TradeSystem objectAtIndex:index];
			 		
			num = (NSDecimalNumber *)[NSDecimalNumber numberWithFloat:[[tData objectForKey:@"RUealizeGainLoss"] floatValue]];		
			
		}
		if(fieldEnum== CPTTradingRangePlotFieldX) 
			num = (NSDecimalNumber *) [NSDecimalNumber numberWithInt:index];		
	}	
	if ([(NSString *)plot.identifier isEqualToString:@"Plot3"]) {
		if ( fieldEnum == CPTScatterPlotFieldY ) {
			
			NSDictionary *tData = (NSDictionary *)[stockdata.financialData_TradeSystem objectAtIndex:index];
			 			
			num = (NSDecimalNumber *)[NSDecimalNumber numberWithFloat:[[tData objectForKey:@"RealizeGainLoss"] floatValue]];		
			
		}
		if(fieldEnum== CPTTradingRangePlotFieldX) 
			num = (NSDecimalNumber *) [NSDecimalNumber numberWithInt:index];		
	}		
	
	return num;	   	  
 
}

#pragma mark -
#pragma mark  計算最高與最低
-(NSDecimalNumber *)HighPrice:(NSString *)name{
	NSDecimalNumber *outc;	
	outc=[NSDecimalNumber notANumber];	
	
	for (int i=0; i< [stockdata.financialData_TradeSystem count]-1 ; i++) {
		NSArray *financialData = stockdata.financialData_TradeSystem;
		NSDictionary *fData = (NSDictionary *)[financialData objectAtIndex:i];
		
		NSDecimalNumber *high = [fData objectForKey:name];	
		if ( [outc isEqual:[NSDecimalNumber notANumber]] ) {
			outc= high;
		}		
		if ( [high compare:outc] == NSOrderedDescending ) {
			outc = high;
		}		
	}
	return [self turnNSDecimalNumber:outc];
}
-(NSDecimalNumber *)LowPrice:(NSString *)name{
	NSDecimalNumber *outc;	
	outc=[NSDecimalNumber notANumber];	
	
	for (int i=0; i< [stockdata.financialData_TradeSystem count]-1 ; i++) {
		NSArray *financialData = stockdata.financialData_TradeSystem;
		NSDictionary *fData = (NSDictionary *)[financialData objectAtIndex:i];
		
		NSDecimalNumber *low = [fData objectForKey:name];	
		if ( [outc isEqual:[NSDecimalNumber notANumber]] ) {
			outc= low;
		}		
		if ( [low compare:outc] == NSOrderedAscending ) {
			outc = low;
		}		
	}
	return [self turnNSDecimalNumber:outc];
}
-(NSDecimalNumber *)turnNSDecimalNumber:(NSDecimalNumber *)inp{
	NSString *h;
	//h=[[NSString alloc] init];
	h=[inp stringValue];
	inp=[NSDecimalNumber decimalNumberWithString:h];
	//[h autorelease];
	return inp;
}

-(NSString *)LableDateString:(int)Index{
	
	if (Index<[stockdata.financialData count]) {
		NSArray *financialData = stockdata.financialData;
		NSDictionary *fData = (NSDictionary *)[financialData objectAtIndex:Index];
		NSDate *theDate= [fData objectForKey:@"date"];	
		
		NSDateFormatter* df = [[NSDateFormatter alloc]init];
		[df setDateFormat:@"yy/MM"];
		NSString *result = [df stringFromDate:theDate];
		[df release];	
		return result;		
	}
	else {
		return @"na";
	}
	
	
	
}



@end

